package com.zhl.study.entity;

import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
import org.telegram.telegrambots.meta.api.methods.send.SendVideo;
import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardButton;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @description Telegram提供两种与机器人通信的方式getUpdates和webhooks
 * getUpdates的原理是通过长轮询去请求bot获取别人发送给bot的消息，
 * webhooks的原理是每次有人发送消息给bot的时候telegram会第一时间推送到你的服务器上，
 * webhooks的时效性原理上要比getUpdates的方式高，但是webhooks方式去实现机器人需要你的服务器有一个域名和https证书，
 * 通过实践观察getUpdates的方式基本上也会第一时间能拿到别人发送给bot的消息，时效性不会太差，
 * 所以这次教程用getUpdates的方式去实现。
 * @Author: z156940l@outlook.com
 * @className PhotoMassage
 * @Date: 2024年4月25日
 */
public class MyBot extends TelegramLongPollingBot {
    private String token = "7020437248:AAE_E1DDublr8vpetNu8NkKqSkNqRdremXs";
    private String botUsername = "lingzifu_bot";


    @Override
    public String getBotUsername() {
        return this.botUsername;
    }

    @Override
    public String getBotToken() {
        return this.token;
    }

    @Override
    public void onUpdateReceived(Update update) {
        System.out.println(update);
        /** 通过打印update对象可以看到别人给济切尼发送的消息
        System.out.println(update);
         */
        /** 通过下面代码可以通过机器人给别人回复消息
        SendMessage message = SendMessage.builder()
                .text("hello!")
                .chatId(update.getMessage().getChatId().toString())
                .build();
        try {
            execute(message);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
         */

        /** 给别人回复一个选择框
        InlineKeyboardButton button1 = InlineKeyboardButton.builder().text("A").callbackData("opt1").build();
        InlineKeyboardButton button2 = InlineKeyboardButton.builder().text("B").callbackData("opt2").build();
        InlineKeyboardButton button3 = InlineKeyboardButton.builder().text("C").callbackData("opt3").build();
        InlineKeyboardButton button4 = InlineKeyboardButton.builder().text("D").callbackData("opt4").build();
        List<InlineKeyboardButton> list1 = new ArrayList<>();
        List<InlineKeyboardButton> list2 = new ArrayList<>();
        List<InlineKeyboardButton> list3 = new ArrayList<>();
        List<InlineKeyboardButton> list4 = new ArrayList<>();
        list1.add(button1);
        list2.add(button2);
        list3.add(button3);
        list4.add(button4);
        List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
        Collections.addAll(rowList, list1, list2, list3, list4);
        InlineKeyboardMarkup inlineKeyboardMarkup = InlineKeyboardMarkup.builder().keyboard(rowList).build();
        SendMessage message = SendMessage.builder()
                .text("请选择一个选项!")
                .chatId(update.getMessage().getChatId().toString())
                .replyMarkup(inlineKeyboardMarkup)
                .build();
        try {
            execute(message);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
         */

        /** 获取键盘选择
        if (update.getCallbackQuery().getData().equals("opt1")) {
            try {
                KeyboardButton button1 = KeyboardButton.builder().text("1").build();
                KeyboardButton button2 = KeyboardButton.builder().text("2").build();
                KeyboardButton button3 = KeyboardButton.builder().text("3").build();
                KeyboardButton button4 = KeyboardButton.builder().text("4").build();
                KeyboardRow keyboardRow1 = new KeyboardRow();
                KeyboardRow keyboardRow2 = new KeyboardRow();
                keyboardRow1.add(button1);
                keyboardRow1.add(button2);
                keyboardRow2.add(button3);
                keyboardRow2.add(button4);
                List<KeyboardRow> keyboardRows = new ArrayList<>();
                Collections.addAll(keyboardRows, keyboardRow1, keyboardRow2);
                ReplyKeyboardMarkup replyKeyboardMarkup = ReplyKeyboardMarkup.builder().keyboard(keyboardRows).build();
                SendMessage message = SendMessage.builder().replyMarkup(replyKeyboardMarkup).text("你选择了A").chatId(update.getCallbackQuery().getFrom().getId().toString()).build();
                execute(message);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
         */

        /**  设置Markdown&html语法
        SendMessage message = SendMessage.builder()
                .text("点击可以跳到我的博客：[joelwublog](https://www.joelwublog.com)")
                .chatId(update.getMessage().getChatId().toString())
                .parseMode("MarkdownV2")//设置为MarkdownV2语法
                //.parseMode("HTML")//设置为html语法
                .build();
        try {
            execute(message);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
         */

        /** 分享电话和地址
        List<KeyboardRow> list = new ArrayList<>();
        KeyboardRow keyboardRow = new KeyboardRow();
        KeyboardButton keyboardButton1 = KeyboardButton.builder().text("分享电话").requestContact(true).build();
        KeyboardButton keyboardButton2 = KeyboardButton.builder().text("分享位置").requestLocation(true).build();
        keyboardRow.add(keyboardButton1);
        keyboardRow.add(keyboardButton2);
        list.add(keyboardRow);
        ReplyKeyboardMarkup keyboardMarkup = ReplyKeyboardMarkup.builder().keyboardRow(keyboardRow).oneTimeKeyboard(true).build();
        try {
            execute(SendMessage.builder().chatId(update.getMessage().getChatId().toString()).text("点击下面按钮分享电话以及位置").replyMarkup(keyboardMarkup).build());
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
         */

        /** 发送图片
        //发送url地址图片
        SendPhoto sendPhoto = SendPhoto.builder().chatId(update.getMessage().getChatId().toString()).photo(new InputFile("https://joelwublog.com/upload/2022/07/bot-2.png")).build();
        //通过fileId发送telegram服务器上的图片
        //SendPhoto sendPhoto = SendPhoto.builder().chatId(update.getMessage().getChatId().toString()).photo(new InputFile("12313131")).build();
        //发送本地图片
        //SendPhoto sendPhoto = SendPhoto.builder().chatId(update.getMessage().getChatId().toString()).photo(new InputFile(new File("F:\\bot-1.png"))).build();
        try {
            execute(sendPhoto);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
         */

        /** 发送视频
        //发送url地址视频
        SendVideo sendVideo = SendVideo.builder().chatId(update.getMessage().getChatId().toString()).video(new InputFile("http://vfx.mtime.cn/Video/2021/07/10/mp4/210710095541348171.mp4")).build();
        //通过fileId发送telegram服务器上的视频文件
        //SendVideo sendVideo =SendVideo.builder().chatId(update.getMessage().getChatId().toString()).video(new InputFile("12313131")).build();
        //发送本地视频文件
        //SendVideo sendVideo =SendVideo.builder().chatId(update.getMessage().getChatId().toString()).video(new InputFile(new File("F:\\bot-test.mp4"))).build();
        try {
            execute(sendVideo);
        } catch (TelegramApiException e) {
            e.printStackTrace();
        }
         */


    }

    /**
     * 如果使用Markdown语法消息中有特殊字符需要对特殊字符转义，不然会解析报错
     * @param s
     * @return
     */
    public String escape(String s) {
        if (s == null) return null;
        List<String> list = Arrays.asList("#", "!", "*", "_", "~", "+", "-", ">", "<", "[", "]", "`", ".", "^", "=");
        for (String l : list) {
            s = s.replace(l, "\\" + l);
        }
        return s;
    }

    public MyBot(DefaultBotOptions botOptions) {
        super(botOptions);
    }

    public MyBot() {
    }
}
